package com.yingli.component.service;

import android.app.Service;
import android.content.Intent;
import android.os.Binder;
import android.os.IBinder;
import android.os.Message;
import android.util.Log;

import androidx.annotation.Nullable;

import com.yingli.component.eventbus.MessageEvent;

import org.greenrobot.eventbus.EventBus;

public class FirstService extends Service {
    private MyBinder myBinder;
    //此方法会在bindService的时候用到
    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        Log.i("service","onBind");
        myBinder = new MyBinder();
        //做一些数据的处理
        myBinder.setData("this is changed data");
        return myBinder;
    }

    class  MyBinder extends Binder{
        private String data;
        public String getData(){
            return data==null?"this is the service":data;
        }

        public void setData(String data){
            this.data = data;
        }
    }

    @Override
    public boolean onUnbind(Intent intent) {
        Log.i("service","onUnBind");
        return super.onUnbind(intent);
    }

    @Override
    public void onRebind(Intent intent) {
        super.onRebind(intent);
        Log.i("service","onRebind");
    }

    //第一次初始化的时候会调用
    @Override
    public void onCreate() {
        super.onCreate();
        Log.i("service","onCreate");
    }

    //执行startService 会执行此方法，多次执行startService（服务运行状态，没有destroy），会多次执行此方法
    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
//        Log.i("service","onStartCommand");
//        String data = intent.getStringExtra("data");
//        Log.i("serviceData",data);
//        EventBus.getDefault().register(this);

//        startForeground();
        //执行完此方法后，会自动结束运行
//        stopSelf();
        return super.onStartCommand(intent, flags, startId);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        Log.i("service","onDestroy");
    }
}
